// pages/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    no_face:'/static/defalut_face.png',
    status: 1,
    showPayBox: false,
    showAddPrice:false,
    id:0,
    timesPop:0,
    info:{id:0,order_log:{},imgs:[]},
    sub:{price:'',note:''}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id
    })
  },
  onShow() {
    this.load();
  },
  onPullDownRefresh() {
    this.load();
    wx.stopPullDownRefresh();
  },
  sh(){
    wx.navigateTo({
      url:"/pages/my/refund?id="+this.data.id
    })
  },
  closeAdd(){
    this.setData({
      showAddPrice:false
    })
  },
  addPrice(){
    this.setData({
      showAddPrice:true
    })
  },
  setInput(e){
    let key=e.currentTarget.dataset.key;
    this.setData({
      [key]:e.detail.value
    })
  },
  async subAdd(){
    let sub=this.data.sub;
    sub.id=this.data.id;
    if(sub.price==""){
      wx.util.toast('请填写金额');
      return '';
    }
    await wx.util.post("order/add",sub,false);
    wx.setStorageSync("order_tab",-1);
    this.setData({
      'sub.price':'',
      'sub.note':''
    })
    this.closeAdd();
    await this.load();
    this.btnPay();
  },
  async load(){
    let res=await wx.util.post("order/detail",{id:this.data.id},false);
    this.setData({
      info:res.data
    })
  },
  async del(){
    await wx.util.post("order/del",{id:this.data.id});
    wx.setStorageSync("order_tab",-1);
    wx.switchTab({
      url:"/pages/myOrder/myOrder"
    })
  },
  ystg(){
    wx.util.ask('是否确认验收通过？',this.orderOk);
  },
  async orderOk(){
    wx.setStorageSync("order_tab",-1);
    await wx.util.post("order/accept",{id:this.data.id});
    wx.util.toast('验收成功');
    this.load();
  },
  detail(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url:"/pages/productDetail/productDetail?id="+id
    })
  },
  callWorker(){
    wx.makePhoneCall({
      phoneNumber: this.data.info.workerTel
    });
  },
  fapiao(){
    if(this.data.info.status!=5){
      wx.util.toast('订单完成后才可开具发票');
      return;
    }
    wx.navigateTo({
      url:"/pages/my/invoice/invoice"
    })
  },
  getImg(s){
    if(s==8){
      return '/static/images/img_order_closed3x.png';
    }
    if(s==1){
      return '/static/images/img_order_payment3x.png';
    }
    if(s==2){
      return '/static/images/img_order_waiting.png';
    }
    if(s==5){
      return '/static/images/img_order_signed3x.png';
    }
    return '';
  },
  logistics() {
    wx.util.href("/pages/logistics/logistics?id="+this.data.id);
  },
  btnPay() {
    this.setData({
      timesPop:this.data.timesPop++,
      showPayBox:true
    })
  },
  closePayBox() {
    this.setData({
      showPayBox:false
    })
    this.load();
  },
  refund(){
    wx.util.href("/pages/my/refund/refund");
  }
})